<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="证书名称" prop="cerName">
        <el-input
          v-model="queryParams.cerName"
          placeholder="请输入证书名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书目录" prop="cerItemCode">
        <el-input
          v-model="queryParams.cerItemCode"
          placeholder="请输入证书目录代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主办单位" prop="cerOrganizer">
        <el-input
          v-model="queryParams.cerOrganizer"
          placeholder="请输入主办单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="证书名称" align="center" prop="cerName" />
      <el-table-column label="证书目录代码" align="center" prop="cerItemCode" />
      <el-table-column label="证书目录名称" align="center" prop="cerItemCode" />
      <el-table-column label="证书介绍" align="center" prop="cerDesc" />
      <el-table-column label="主办单位" align="center" prop="cerOrganizer" />
      <el-table-column label="考试时间" align="center" prop="examTime" />
      <el-table-column label="考试费用" align="center" prop="examFee" />
      <el-table-column label="考试科目" align="center" prop="examSubjects" />
      <el-table-column label="推荐考证时间" align="center" prop="appropriateTime" />
      <el-table-column label="测评加分" align="center" prop="cerBonusPoint" />
      <el-table-column label="比例因子" align="center" prop="cerFactor" />
      <el-table-column label="相关课程" align="center" prop="relatedCourses" />
      <el-table-column label="学分置换" align="center" prop="creditExchange" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:info:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改证书信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="证书名称" prop="cerName">
          <el-input v-model="form.cerName" placeholder="请输入证书名称" />
        </el-form-item>
        <el-form-item label="证书目录" prop="cerItemCode">
          <el-tree-select
                  v-model="form.cerItemCode"
                  :data="itemOptions"
                  :props="{ value: 'itemCode', label: 'itemName', children: 'children' }"
                  value-key="itemCode"
                  placeholder="选择证书目录"
                  check-strictly
          />
        </el-form-item>
        <el-form-item label="证书介绍" prop="cerDesc">
          <el-input v-model="form.cerDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="主办单位" prop="cerOrganizer">
          <el-input v-model="form.cerOrganizer" placeholder="请输入主办单位" />
        </el-form-item>
        <el-form-item label="考试时间" prop="examTime">
          <el-input v-model="form.examTime" placeholder="请输入考试时间，如一年两次，上半年一次，下半年一次" />
        </el-form-item>
        <el-form-item label="考试费用" prop="examFee">
          <el-input v-model="form.examFee" placeholder="请输入考试费用" />
        </el-form-item>
        <el-form-item label="考试科目" prop="examSubjects">
          <el-input v-model="form.examSubjects" placeholder="请输入考试科目" />
        </el-form-item>
        <el-form-item label="推荐考证时间" prop="appropriateTime">
          <el-input v-model="form.appropriateTime" placeholder="请输入推荐考证时间，如大一/大二/大三/大四" />
        </el-form-item>
        <el-form-item label="测评加分" prop="cerBonusPoint">
          <el-input v-model="form.cerBonusPoint" placeholder="请输入测评加分" />
        </el-form-item>
        <el-form-item label="比例因子" prop="cerFactor">
          <el-input v-model="form.cerFactor" placeholder="请输入比例因子" />
        </el-form-item>
        <el-form-item label="相关课程" prop="relatedCourses">
          <el-input v-model="form.relatedCourses" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="学分置换" prop="creditExchange">
          <el-input v-model="form.creditExchange" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/cer/cerInfo";
import { listItem } from "@/api/cer/cerItem";

const { proxy } = getCurrentInstance();

const infoList = ref([]);
const itemOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cerName: null,
    cerItemCode: null,
    cerDesc: null,
    cerOrganizer: null,
    examTime: null,
    examFee: null,
    examSubjects: null,
    appropriateTime: null,
    cerBonusPoint: null,
    cerFactor: null,
    relatedCourses: null,
    creditExchange: null,
    createrId: null,
    updaterId: null,
  },
  rules: {
    cerName: [
      { required: true, message: "证明名称不能为空", trigger: "blur" }
    ],
    cerItemCode: [
      { required: true, message: "证书目录代码不能为空", trigger: "blur" }
    ],
    cerBonusPoint: [
      { required: true, message: "测评加分不能为空", trigger: "blur" }
    ],
    cerFactor: [
      { required: true, message: "比例因子不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询证书信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    cerName: null,
    cerItemCode: null,
    cerDesc: null,
    cerOrganizer: null,
    examTime: null,
    examFee: null,
    examSubjects: null,
    appropriateTime: null,
    cerBonusPoint: null,
    cerFactor: null,
    relatedCourses: null,
    creditExchange: null,
    remark: null,
    createrId: null,
    updaterId: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  listItem().then(response => {
    itemOptions.value = proxy.handleTree(response.data, "id");
  });
  open.value = true;
  title.value = "添加证书信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  listItem().then(response => {
    itemOptions.value = proxy.handleTree(response.data, "id");
  });
  getInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改证书信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除证书信息编号为"' + _ids + '"的数据项？').then(function() {
    return delInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

getList();
</script>
